package com.sishu.websocket4.controller;

import cn.dev33.satoken.util.SaResult;
import com.sishu.websocket4.pojo.cardInformation;
import com.sishu.websocket4.service.Cardservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/card/")
@CrossOrigin(origins = "*")
public class CardController {

@Resource
private Cardservice cardservice;


    //随机获取一道题库中的题目
    @RequestMapping("getramtcard")
    public SaResult getramtiku(){
      return  cardservice.getRandomcard();
    }
//
//    //回答随机抽取的题目
//    @RequestMapping("answerramtiku")
//    public SaResult answerramtiku(@Param("questionId")String questionId,@Param("answer")String answer){
//        return  tikuservice.Tikuanswer(questionId,answer);
//    }
    //增加题目
    @RequestMapping("insert")
    public SaResult insertcardinformation(@RequestBody List<cardInformation> cards){
        //循环打印传过来的题目是不是在数据库中
        boolean flag = true;//默认是没有在数据库中
        SaResult selectcard =SaResult.ok();
        for (cardInformation card : cards) {
            selectcard = cardservice.selectcard(card);
            System.out.println("selecttiku = " + selectcard);

            if (selectcard.getCode()==500){
                flag = false;//题库中有此题目
                return selectcard;
            }
        }

        if (selectcard.getCode()==200){
            //   插入题目
            System.out.println("tiku =" + cards);
            return cardservice.insertcardinformation(cards);
        }

            return selectcard;
    }



}


